package com.eclecticconceptions.vulcan;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.EditText;

public class Cp12Utils {
	
	public static final String PREFS_FILE = "prefs_file";
	private static final double NATURALCV=38.6, PROPANECV=95.8, BUTANECV=126;//all in Mj/m3; 1kW = 1000Joules/sec
	public static int gasCode=1;
	public static int meterSizeFt=1;
	public static String errorMsg="";

	public static double getGasCv(int gasCode){
		switch (gasCode){
		case 1:
			return NATURALCV;
		case 2:
			return PROPANECV;
		case 3:
			return BUTANECV;
		default:
			return 0.0;
		}
	}
	
	public static double getImperialGasRates(int timeSecs){
		double kW=0;
			
		if (timeSecs==0){ //zero results table
			kW=0;
		} else {
			//convert seconds to use meter vol(ft3) of gas into m3/hr
			double ft3PerHr=(meterSizeFt*3600)/timeSecs;
			double m3PerHr = ft3PerHr*0.0283168466;//0.0283347;
			//m3/hr * metric CV gives Mj/hr; convert to J/s = kW
			double mjPerHr = m3PerHr*getGasCv(gasCode);
			kW=mjPerHr/3.6;
		}
		return kW;
	}
	
	public static double getMetricGasRates(int timeSecs, double startReading,
			double endReading){
		double kW=0;
		//gas used m3 in 120secs or so; convert to m3/hr; multiply by CV;
		double gasUsed = endReading-startReading;
		//convert to m3/hr
		double gasUsedPerHr=(gasUsed*3600)/timeSecs;
		double mjPerHr=gasUsedPerHr*getGasCv(gasCode);
		kW=mjPerHr/3.6;
		return kW;
	}
	
	public static String formatTime(int timeSecs){
		int mMinutes;
		int mSeconds;
		String displayTime;
		
		if (timeSecs>3600)timeSecs-=3600;
		
		mMinutes=timeSecs/60;
		mSeconds=timeSecs%60;
		
		if(mSeconds<10){
			displayTime=""+mMinutes+":0"+mSeconds;
		} else{
			displayTime=""+mMinutes+":"+mSeconds;
		}
		return displayTime;

	}
	
	public static double getDoubleEditText(EditText editText){
		try{
			return Double.parseDouble(editText.getText().toString());
		}
		catch(Exception e){
			return 0;
		}
	}
	
	public static double round2(double num) {
		double result = num * 100;
		result = Math.round(result);
		result = result / 100;
		return result;
		}
	
	public static void alertbox(String title, String mymessage)
	   {
	   new AlertDialog.Builder(null)
	      .setMessage(mymessage)
	      .setTitle(title)
	      .setCancelable(true)
	      .setNeutralButton(android.R.string.cancel,
	         new DialogInterface.OnClickListener() {
	         public void onClick(DialogInterface dialog, int whichButton){}
	         })
	      .show();
	   }
	// see http://androidsnippets.com/display-an-alert-box
}
